const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');
const common = require('../../utils/common.js');
const app = getApp()
Page({
  data: {
    //定时器
    time: '',
    //顶部选项卡
    selFunction: 2,
    //控制选项卡固定顶部
    fixed: false,
    //控制积分页面明细和说明的切换
    selJf: 1,
    //是否打开赚积分模块
    zJf: true,
    //是否打开扣积分模块
    kJf: true,
    //是否打开积分有效期模块
    yxq: true,
    //积分明细模块切换
    jfMx: 1,
    //积分等级
    jfLv: 0,
    //账号总橙贝
    cbSum: 0,
    //还差多少橙贝领奖
    surplus: 5000,
    //控制积分块动态
    jiujiu: 0,
    //控制提现说明窗口
    txts: false,
    //客服微信号
    kefuwx: "xingqubaike",
    //赚积分状态
    zjfOpen: { "xx": true, "rz": true, "jj": true, "cjqz": true, "wsqz": true, "cyqz": true, "fbrj": true, "rjdz": true, "plrj": true, "yzrj": true, "yqxr": true, "txxr": true, "fx": true,},
    //累积情况数据
    addCBarr: [{ 'typeId': '1', 'userName': '', 'cb': '2000', 'time': '2018-06-19 23:07' }, { 'typeId': '4', 'userName': '', 'cb': '5', 'time': '2018-06-19 23:06' }],
    //兑换记录数据
    useCBarr: [{ 'url': 'https://image.xingqubaike.com:8443/minicard/ucenter/personhome/homesetup/xigua.png', 'name': '测试数据', 'cb': '20000', 'time': '2018-06-19 23:07' }, { 'url': 'https://image.xingqubaike.com:8443/minicard/ucenter/personhome/homesetup/xigua.png', 'name': '测试数据', 'cb': '2000', 'time': '2018-06-19 23:07' }, { 'url': 'https://image.xingqubaike.com:8443/minicard/ucenter/personhome/homesetup/xigua.png', 'name': '测试数据', 'cb': '2000', 'time': '2018-06-19 23:07' }],
    //控制对话框的显示
    dhkShow: false,
    //控制对话框的内容显示
    dhkTs: null,
    //徽章总数
    badgesum: 0,
    //收益数据
    earnings: { "money": '0.82', 'dayMoney': '0.00', "waitMoney": '299.85', "winMoney": '149.00' },
    //成长徽章数据
    upbadge: { "sum": 0, "vip": '0', "vipjd": "0", "xxcz": "0", "xxczjd": "0", "xqfx": "0", "xqfxjd": "0", "cbdh": "0", "cbdhjd": "0", "xqzj": "0", "xqzjjd": "0", "tbgx": "0", "tbgxjd": "0"},
    //成就徽章数据
    cjbadge: { "sum": 0, "qz": false, "sh": false, "yd": false, "ms": false, "jy": false, "jk": false, "yund": false, "cy": false, "ly": false, "ss": false, "cf": false, "yl": false, "hy": false, "hlw": false, "gy": false, "jn": false, "sm": false, },
    //收益记录数据
    winMoneyList: [{ 'sum': '50.00', 'time': '2019-01-15 17:59:32' }, { 'sum': '50.00', 'time': '2019-01-16 17:59:32' },{ 'sum': '40.00', 'time': '2019-01-17 17:59:32' }],
  },
  //加载用户
  onLoad: function (options) {
    var _this = this
    if (app.globalData.logininfo != null && app.globalData.logininfo!=''){
      _this.setData({
        logininfo: app.globalData.logininfo,
        openid: app.globalData.logininfo.openid,
      });
      var openid = app.globalData.logininfo.openid;
      var data = {};
      if (openid != null && openid != "") {
        util.post(api.fetchToken()).then(function (res) {
          if (res.access_token != '') {
            return Promise.resolve({
              access_token: res.access_token
            });
          }
        }).then(function (res) {
          data.openid = openid;
          data.access_token = res.access_token;
          //获取用户信息
          util.post(api.GetUserInfoByHomepage, data).then(function (res) {
            if (res.code == 200) {
              _this.setData({
                userData: res.result.json,
              })
              _this.setData({
                jfLv: _this.data.userData.energySize,
                cbSum: _this.data.userData.enableIntegral,
                surplus: 5000 - (_this.data.userData.enableIntegral%5000)
              })
            }
          });
        });
      }
    }
  },
  onShareAppMessage() {
    return {
      title: '好玩的百科知识平台',
      desc: '',
      path: '/pages/home/home'
    }
  },
  onShow: function(){
    var _this = this
    if (app.globalData.logininfo != null && app.globalData.logininfo != '') {
      _this.setData({
        logininfo: app.globalData.logininfo,
        openid: app.globalData.logininfo.openid,
      });
    }
    if (app.switchData == 1) {
      _this.setData({
        selFunction: app.switchData
      });
      _this.donghua();
      app.switchData = 99999;
    } 
  },
  //动画
  donghua: function(){
    var _this = this
    if (_this.data.selFunction==1){
      var time =  setTimeout(function () {
        var jj = _this.data.jiujiu
        if (jj != 15) {
          var newjj = parseInt(jj) + 1
          _this.setData({
            jiujiu: newjj
          })
        } else {
          _this.setData({
            jiujiu: 0
          })
        }
        _this.donghua();
      }, 100) 
      _this.setData({
        time: time
      })
    }
  },
  //销毁定时器
  onHide: function () {
    var _this = this
    clearInterval(_this.data.time);
  },
  onUnload: function () {
    var _this = this
    clearInterval(_this.data.time);
  },
  //页面监听
  onPageScroll: function (e) {
    var _this = this;
    var name = this.data.nickName;
    var scrollTop = parseInt(e.scrollTop);
    var query = wx.createSelectorQuery();
    if (scrollTop >= 1) {
      _this.setData({
        fixed: true
      })
    } else {
      _this.setData({
        fixed: false
      })
    }
  },
  //选择功能页面
  selectfunction: function(e){
    var _this = this
    var sel = e.currentTarget.dataset.id
    _this.setData({
      selFunction: sel
    })
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0,
        duration: 1000
      })
    }
    _this.donghua();
  },
  //控制积分页面明细和说明的切换
  selectjifenmk: function(){
    var _this = this
    var sel = _this.data.selJf;
    if(sel==1){
      _this.setData({
        selJf: 2
      })
    }else{
      _this.setData({
        selJf: 1
      })
    }
  },
  //赚积分模块开启关闭
  openzjf: function(){
    var _this = this
    var open = _this.data.zJf
    if (open){
      _this.setData({
        zJf: false
      })
    }else{
      _this.setData({
        zJf: true
      })
    }
  },
  //扣积分模块开启关闭
  openkjf: function(){
    var _this = this
    var open = _this.data.kJf
    if (open) {
      _this.setData({
        kJf: false
      })
    } else {
      _this.setData({
        kJf: true
      })
    }
  },
  //有效期模块开启关闭
  openyxq: function(){
    var _this = this
    var open = _this.data.yxq
    if (open) {
      _this.setData({
        yxq: false
      })
    } else {
      _this.setData({
        yxq: true
      })
    }
  },
  //积分明细模块切换
  seljfmx: function (e) {
    var _this = this
    var eId = e.currentTarget.dataset.id
    _this.setData({
      jfMx: eId
    })
  },
  //打开对话框
  opendhk: function(e){
    var _this = this
    var oId = e.currentTarget.dataset.id
    _this.setData({
      dhkTs: oId,
      dhkShow: true
    })
  },
  //关闭对话框
  closedhk: function(){
    var _this = this
    _this.setData({
      dhkTs: null,
      dhkShow: false
    })
  },
  //加载更多收益记录
  syloadall: function(){
    wx.showToast({
      title: '数据加载中...',
      icon:'loading',
      duration: 1500
    })
  },
  //提现说明开启
  opentxts: function(){
    var _this = this
    _this.setData({
      txts: true
    })
  },
  //提现说明关闭
  closetxts: function () {
    var _this = this
    _this.setData({
      txts: false
    })
  },
  //复制微信号
  copywx: function(){
    var _this = this
    var kfwx = _this.data.kefuwx
    wx.setClipboardData({
      data: kfwx
    })
  },
  //跳转成长徽章详情
  gotobadge1: function(){
    wx.navigateTo({
      url: "../../grade/pages/grade/badge/badge?h_id=1"
    })
  },
  gotobadge2: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/badge/badge?h_id=2"
    })
  },
  gotobadge3: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/badge/badge?h_id=3"
    })
  },
  gotobadge4: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/badge/badge?h_id=4"
    })
  },
  gotobadge5: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/badge/badge?h_id=5"
    })
  },
  gotobadge6: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/badge/badge?h_id=6"
    })
  },
  gotogoal1: function(){
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=1"
    })
  },
  gotogoal2: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=2"
    })
  },
  gotogoal3: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=3"
    })
  },
  gotogoal4: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=4"
    })
  },
  gotogoal5: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=5"
    })
  },
  gotogoal6: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=6"
    })
  },
  gotogoal7: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=7"
    })
  },
  gotogoal8: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=8"
    })
  },
  gotogoal9: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=9"
    })
  },
  gotogoal10: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=10"
    })
  },
  gotogoal11: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=11"
    })
  },
  gotogoal12: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=12"
    })
  },
  gotogoal13: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=13"
    })
  },
  gotogoal14: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=14"
    })
  },
  gotogoal15: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=15"
    })
  },
  gotogoal16: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=16"
    })
  },
  gotogoal17: function () {
    wx.navigateTo({
      url: "../../grade/pages/grade/goal/goal?h_id=17"
    })
  },
  //跳转提现页面
  gotowinMoney: function() {
    wx.navigateTo({
      url: "../../grade/pages/grade/winMoney/winMoney"
    })
  },
  //跳转明细页面
  gotodetailMoney: function() {
    wx.navigateTo({
      url: "../../grade/pages/grade/detailMoney/detailMoney"
    })
  },
  //跳转新建圈子
  doPublish: function (event) {
    wx.navigateTo({
      url: '../../ucenter/pages/ucenter/personcenter/publish/publish?userid=1',
    })
  },
  //跳转个人资料
  doHomesetup: function (event) {
    wx.navigateTo({
      url: '../../ucenter/pages/ucenter/personinfo/personinfo?userid=1',
    })
  },
  //跳转抽奖
  gotoprize: function(event) {
    wx.navigateTo({
      url: '../../grade/pages/grade/prize/prize',
    })
  },
  /**
   * 每日签到    
   */
  toSign: function (e) {
    wx.navigateTo({
      url: '../../grade/pages/grade/sign/sign'
    })
  },
  //跳转登陆
  gotologin: function () {
    wx.navigateTo({
      url: '../ucenter/personcenter/login/login',
    })
  },
  //跳转兴趣圈
  gotointerest: function(){
    wx.switchTab({
      url: '/pages/interest/interest',
    });
  }
})